package com.wanmait.recycle.redis.dao.impl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import com.wanmait.recycle.redis.dao.RedisViewCountDao;

@Repository
public class RedisViewCountDaoImpl implements RedisViewCountDao {

	@Resource
	private RedisTemplate<String, Object> redisTemplate;
	
	/*文章被浏览*/
	@Override
	public void addArticleView(Integer articleId, String ip) {
		
		redisTemplate.opsForSet().add("viewArticle:"+articleId+":clientIp:set", ip);
		//文章浏览时，记录下当前时间，下次浏览，判断是否超过24小时
		Timestamp nowTime = new Timestamp(System.currentTimeMillis());
		redisTemplate.opsForValue().set("viewArticle:"+articleId+":clientIp:"+ip+":time",nowTime.toString());
	}

	/*判断某篇文章是否被某用户浏览过&&浏览时间是否超过24小时*/
	/*此处返回为是否可以添加浏览，true为可以，false为不可以*/
	@Override
	public boolean isViewArticle(Integer articleId, String ip) {
		
		//该ip对该文章是否浏览过
		boolean isExist = redisTemplate.opsForSet().isMember("viewArticle:"+articleId+":clientIp:set", ip);
		if(isExist) {
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Timestamp time = new Timestamp(System.currentTimeMillis());
			time = Timestamp.valueOf(redisTemplate.opsForValue().get("viewArticle:"+articleId+":clientIp:"+ip+":time").toString());
			//如果浏览过，判断时间是否超过24小时
			Long nowTime = System.currentTimeMillis();
			Long viewTime = null;
			try {
				viewTime = sdf.parse(time.toString()).getTime();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(nowTime-viewTime>=1000*60*60*24) {
				return true;
			}
			else {
				return false;
			}
		}else {
			return true;
		}
		
	}

	/*文章的浏览次数加1*/
	@Override
	public void increaseArticleViewCount(Integer articleId) {
		redisTemplate.opsForValue().increment("viewArticle:"+articleId+":viewCount", 1);
		
	}

	/*获得文章的浏览次数*/
	@Override
	public int getArticeViewCount(Integer articleId) {
		
		try {
				String praiseCount = (String)redisTemplate.opsForValue().get("viewArticle:"+articleId+":viewCount");
				return Integer.parseInt(praiseCount);
			} 
			catch (NumberFormatException e) {
				return 0;
			}
	}

	/*文章的浏览，24小时清空一次*/
	@Override
	public void removeArticleView(Integer articleId, String ip) {
		redisTemplate.opsForSet().remove("viewArticle:"+articleId+":clientIp:set", ip);
		
	}

	/*哪些文章被浏览过*/
	@Override
	public void addUpdateViewArticle(Integer articleId) {
		redisTemplate.opsForSet().add("viewArticle:update:set", articleId.toString());
		
	}

	/*获得哪些文章被浏览过*/
	@Override
	public Set<Integer> getUpdateViewArticle() {
		//定义类型为Integer的set集合
		Set<Integer> setInts = new HashSet<>();
		//取出redis中的元素
		Set<Object> setObjs = redisTemplate.opsForSet().members("viewArticle:update:set");
		//迭代器(Iterator)是一个对象，它的工作是遍历并选择序列中的对象
		Iterator<Object> it = setObjs.iterator();
		while(it.hasNext())
		{
			setInts.add(Integer.parseInt((String)it.next()));
		}
		return setInts;
	}

	@Override
	public void removeUpdateViewArticle() {
		redisTemplate.opsForSet().remove("viewArticle:update:set");
		
	}

	@Override
	public void addQuestionView(Integer questionid, String ip) {
		// TODO Auto-generated method stub
		redisTemplate.opsForSet().add("viewQuestion:"+questionid+":clientIp:set", ip);
		//文章浏览时，记录下当前时间，下次浏览，判断是否超过24小时
		Timestamp nowTime = new Timestamp(System.currentTimeMillis());
		redisTemplate.opsForValue().set("viewQuestion:"+questionid+":clientIp:"+ip+":time",nowTime.toString());
	}

	@Override
	public boolean isViewQuestion(Integer questionId, String ip) {
		// TODO Auto-generated method stub
			boolean isExist = redisTemplate.opsForSet().isMember("viewQuestion:"+questionId+":clientIp:set", ip);
			if(isExist) {
				
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				Timestamp time = new Timestamp(System.currentTimeMillis());
				time = Timestamp.valueOf(redisTemplate.opsForValue().get("viewQuestion:"+questionId+":clientIp:"+ip+":time").toString());
				//如果浏览过，判断时间是否超过24小时
				Long nowTime = System.currentTimeMillis();
				Long viewTime = null;
				try {
					viewTime = sdf.parse(time.toString()).getTime();
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if(nowTime-viewTime>=1000*60*60*24) {
					return true;
				}
				else {
					return false;
				}
			}else {
				return true;
			}
	}

	@Override
	public void increaseQuestionViewCount(Integer questionId) {
		// TODO Auto-generated method stub
		redisTemplate.opsForValue().increment("viewQuestion:"+questionId+":viewCount", 1);
	}

	@Override
	public int getQuestionViewCount(Integer questionId) {
		// TODO Auto-generated method stub
		try {
			String praiseCount = (String)redisTemplate.opsForValue().get("viewQuestion:"+questionId+":viewCount");
			return Integer.parseInt(praiseCount);
		} 
		catch (NumberFormatException e) {
			return 0;
		}
	}

	@Override
	public void removeQuestionView(Integer questionId, String ip) {
		// TODO Auto-generated method stub
		redisTemplate.opsForSet().remove("viewQuestion:"+questionId+":clientIp:set", ip);
	}

	@Override
	public void addUpdateViewQuestion(Integer questionId) {
		// TODO Auto-generated method stub
		redisTemplate.opsForSet().add("viewQuestion:update:set", questionId.toString());
	}

	@Override
	public Set<Integer> getUpdateViewQuestion() {
		// TODO Auto-generated method stub
		Set<Integer> setInts = new HashSet<>();
		//取出redis中的元素
		Set<Object> setObjs = redisTemplate.opsForSet().members("viewQuestion:update:set");
		//迭代器(Iterator)是一个对象，它的工作是遍历并选择序列中的对象
		Iterator<Object> it = setObjs.iterator();
		while(it.hasNext())
		{
			setInts.add(Integer.parseInt((String)it.next()));
		}
		return setInts;
	}

	@Override
	public void removeUpdateViewQuestion() {
		// TODO Auto-generated method stub
		redisTemplate.opsForSet().remove("viewQuestion:update:set");
	}

}
